from django import forms
from .models import Users
from django.forms import ModelForm
import re

class LoginForm(forms.Form):
    img_code=forms.CharField(widget=forms.TextInput(attrs={'class':'form-control verify-code email-name','placeholder':"图片验证码"}))
    passwd = forms.CharField(widget=forms.PasswordInput(attrs={'class': 'form-control', 'placeholder': "请输入密码"}))
    email_address=forms.CharField(widget=forms.TextInput(attrs={'class':'form-control email-name','placeholder':"邮箱地址"}))

    def clean_email_address(self):  # 函数必须以clean_开头
        mobile = self.cleaned_data['email_address']
        mobile_regex = r'^1[34578]\d{9}$'
        p = re.compile(mobile_regex)
        if p.match(mobile):
            return mobile
        else:
            raise forms.ValidationError('手机号码非法', code='invalid mobile')



class UsersForm(ModelForm):
    class Meta:
        model = Users
        fields = ['mobile', 'password', 'money']
        widgets = {
            'mobile': forms.TextInput(attrs={'class':'form-control email-name','placeholder':"邮箱地址"}),
            'password': forms.PasswordInput(attrs={'class': 'form-control', 'placeholder': "请输入密码"}),
            'money': forms.TextInput(attrs={'class':'form-control verify-code email-name','placeholder':"图片验证码"})
        }

    def clean_mobile(self):  # 函数必须以clean_开头
        mobile = self.cleaned_data['mobile']
        mobile_regex = r'^1[34578]\d{9}$'
        p = re.compile(mobile_regex)
        if p.match(mobile):
            return mobile
        else:
            raise forms.ValidationError('手机号码非法', code='invalid mobile')